﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AdverseEventReportSystem.Utils;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using System.Data;

namespace AdverseEventReportSystem.Web
{
    public class BaseController : Controller
    {
        /// <summary>
        /// 登录用户信息
        /// </summary>
        public SysLoginUser loginUser
        {
            get
            {
                return LoginManager.GetLoginUser();
            }
        }

        public ActionResult GetLoginUserInfo()
        {
            return JsonDate(loginUser);
        }

        /// <summary>
        /// 返回处理过的时间的Json字符串
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public ContentResult JsonDate(object date)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return Content(JsonConvert.SerializeObject(date, Formatting.Indented, timeConverter));
        }

        public ContentResult JsonFilerNull(object date)
        {
            var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

            return Content(JsonConvert.SerializeObject(date, Formatting.Indented, jSetting));
        }

        /// <summary>
        /// 获取职称
        /// </summary>
        /// <param name="isAll"></param>
        /// <returns></returns>
        public ActionResult GetPositionlist(int isAll = 0)
        {
            DataTable dt = new DataTable();
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable serviceResult = service.getPositionlist("", "", "1000");

            if (serviceResult.S)
            {
                dt = serviceResult.D;
                if (isAll > 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["PositionCode"] = "-1";
                    dr["PositionTitle"] = "请选择职称";
                    dt.Rows.InsertAt(dr, 0);
                }
            }

            return JsonDate(dt);
        }

        /// <summary>
        /// 获取职务
        /// </summary>
        /// <param name="isAll"></param>
        /// <returns></returns>
        public ActionResult GetJoblist(int isAll = 0)
        {
            DataTable dt = new DataTable();
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable serviceResult = service.getjoblist("", "", "1000");

            if (serviceResult.S)
            {
                dt = serviceResult.D;
                if (isAll > 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["JobCode"] = "-1";
                    dr["JobTitle"] = "请选择职务";
                    dt.Rows.InsertAt(dr, 0);
                }
            }

            return JsonDate(dt);
        }

        /// <summary>
        /// 获取所属科室/病区
        /// </summary>
        /// <param name="isAll">是否添加全部这个选项</param>
        /// <returns></returns>
        public ActionResult GetDepartmentList(int isAll = 0)
        {
            string[] deapartCodeList = loginUser.DepartCode.Split('&');
            string[] deapartNameList = loginUser.DepartName.Split('&');

            List<ComboBoxItem> itemList = new List<ComboBoxItem>();
            if (isAll > 0)
            {
                itemList.Add(new ComboBoxItem() { value = "-1", text = "全部" });
            }
            for (int i = 0; i < deapartCodeList.Length; i++)
            {
                itemList.Add(new ComboBoxItem() { value = deapartCodeList[i], text = deapartNameList[i] });
            }

            return JsonDate(itemList);
        }

        /// <summary>
        /// 获取所有病区
        /// </summary>
        /// <param name="isAll"></param>
        /// <returns></returns>
        public ActionResult GetAllUnits(int isAll = 0)
        {
            DataTable dt = null;
            OperateResult resultInfo = DepartmentManager.GetAllUnits();
            if (resultInfo.Result == 1)
            {
                dt = resultInfo.Body as DataTable;
            }
            if (isAll == 1)
            {
                if (dt == null)
                {
                    dt.Columns.Add("DepartmentCode");
                    dt.Columns.Add("DepartmentName");
                }
                DataRow dr = dt.NewRow();
                dr["DepartmentCode"] = -1;
                dr["DepartmentName"] = "请选择";
                dt.Rows.InsertAt(dr, 0);
            }
            return JsonDate(dt);
        }

        /// <summary>
        /// 获取护士列表
        /// </summary>
        /// <param name="departmentCode"></param>
        /// <param name="isAll"></param>
        /// <returns></returns>
        public ActionResult GetNurseList(string departmentCode = "", int isAll = 0)
        {
            if (departmentCode == "")
            {
                departmentCode = loginUser.CurrDepCode;
            }
            DataTable dt = new DataTable();
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable result = service.getDepartmentCodeNurse(departmentCode);
            if (result.S)
            {
                dt = result.D;
                if (isAll > 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["UserId"] = "-1";
                    dr["UserName"] = "请选择";
                    dt.Rows.InsertAt(dr, 0);
                }
            }
            return JsonDate(dt);
        }

        /// <summary>
        /// 根据字典名称获取字典内容
        /// </summary>
        /// <param name="dictName"></param>
        public ActionResult GetBaseCommonDict(string dictName, int isAll = 0)
        {
            DataTable dt = new DataTable();
            AdverseEventReportSystem.BLL.EscortManageSys.EscortManageSysSoapClient service = new AdverseEventReportSystem.BLL.EscortManageSys.EscortManageSysSoapClient();
            AdverseEventReportSystem.BLL.EscortManageSys.ResultHelperOfDataTable serviceResult = service.GetBaseCommonDict(dictName);
            if (serviceResult.S)
            {
                dt = serviceResult.D;
                if (isAll > 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["ITEM_CODE"] = "-1";
                    dr["ITEM_NAME"] = "请选择";
                    dt.Rows.InsertAt(dr, 0);
                }
            }

            return JsonDate(dt);
        }
    }
}